import { createRouter, createWebHistory } from 'vue-router'
import { getToken } from '@/utils/cookie'
import { ref } from 'vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/c-oj/home/question'  // 无条件重定向到home页面
    },
    {
      path: "/c-oj/home", 
      name: "home",
      component: () => import("@/views/Home.vue"),
      // 移除requiresAuth，允许未登录用户访问
      meta: { requiresAuth: false },
      // 添加子路由
      children: [
        {
          path: "", // 空路径匹配 /c-oj/home
          redirect: "question" // 默认重定向到 /c-oj/home/question
        },
        {
          path: "/", // 匹配 /c-oj/home/
          redirect: "question" // 重定向到 /c-oj/home/question
        },
        {
          path: "question",
          name: "question",
          component: () => import("@/views/Question.vue"), // 确保这里是 Question.vue
          meta: {showBanner: true}
        },
        {
          path: "exam",
          name: "exam",
          component: () => import("@/views/Exam.vue"), // 确保这里是 Exam.vue
          meta: {showBanner: true}
        },
        {
          path: "user/exam",
          name: "userExam",
          component: () => import("@/views/UserExam.vue"), // 使用已有的Question组件
          meta: { requiresAuth: false } // 这个页面需要登录
        },
        {
          path: "user/detail",
          name: "userDetail",
          component: () => import("@/views/UserDetail.vue"),
          meta: { requiresAuth: true, showBanner: false }
        },
        {
          path: "user/message",
          name: "userMessage",
          component: () => import("@/views/UserMessage.vue"),
          meta: { requiresAuth: true }
        },
      ]
    },
    {
      path: "/c-oj/login",
      name: "login",
      component: () => import("@/views/Login.vue"),
    },
    {
      path: "/c-oj/answer",
      name: "answer",
      component: () => import("@/views/Answer.vue"),
    },
  ],
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 添加调试信息
  console.log('导航守卫检查token:', getToken() ? '有token' : '无token');
  
  // 检查路由是否需要登录权限
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 检查用户是否已登录
    if (!getToken()) {
      console.log('未登录，重定向到登录页');
      // 未登录则跳转到登录页
      next({
        path: '/c-oj/login',
        // 保存原本要去的路径，登录成功后可以跳转回去
        query: { redirect: to.fullPath }
      })
    } else {
      console.log('已登录，正常跳转');
      // 已登录则正常跳转
      next()
    }
  } else {
    console.log('不需要登录权限，正常跳转');
    // 不需要登录权限的页面直接跳转
    next()
  }
})

const dateRange = ref(null) // 用于日期选择器的值

// 处理日期变化
function handleDateChange(val) {
  if (val) {
    params.startTime = val[0]
    params.endTime = val[1]
  } else {
    delete params.startTime
    delete params.endTime
  }
}

function onReset() {
  params.pageNum = 1
  params.pageSize = 9
  params.type = 0
  dateRange.value = null // 清除日期选择器的值
  delete params.startTime
  delete params.endTime
  selectedOption.value = 0
  getExamList()
}

export default router
